import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Delete,
  HttpStatus,
  Query,
  Inject,
  Version,
} from '@nestjs/common';
import { DemoService } from './demo.service';
import { CreateDemoDto } from './dto/create-demo.dto';
import { ApiOperation, ApiResponse } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import { loadEnvConfig } from '@/common/config/loadEnv.config';
import { log } from 'console';
import { SkipThrottle } from '@nestjs/throttler';
import { Public } from '@/common/decorators/public.decorator';

// @SkipThrottle()
@Controller('demo')
export class DemoController {
  constructor(private readonly demoService: DemoService) { }

  @Inject(ConfigService)
  private configService: ConfigService;

  @ApiOperation({ summary: '新增接口', description: 'aaa 描述' })
  @ApiResponse({
    status: HttpStatus.OK,
    description: 'aaa 成功',
    type: String,
  })
  // @ApiBody({ type: CreateDemoDto })
  @Post()
  create(@Body() createDemoDto: CreateDemoDto) {
    console.log('create');
    return this.demoService.create(createDemoDto);
  }

  @Version('2')
  @Post()
  create2(@Body() createDemoDto: CreateDemoDto) {
    console.log('create2');
    return this.demoService.create(createDemoDto);
  }

  @Get()
  findAll() {
    return this.demoService.findAll();
  }

  // @Public()
  @Get('/redis/:id')
  findRedis(@Param('id') id: string) {
    return this.demoService.findRedis(id);
  }

  @Get('/redis2/:id')
  findRedis2(@Param('id') name: string) {
    return this.demoService.findRedis2(name);
  }

  // @SkipThrottle()
  @Get('env')
  findEnv() {
    const env = loadEnvConfig();
    console.log('process.env.PORT');

    console.log('process.env.PORT', process.env.APP_ENV, process.env.NODE_ENV);

    return {
      config: this.configService.get('APP_ENV'),
      DATABASE_URL: this.configService.get('DATABASE_URL'),
      env,
    };
  }

  @Get(':id')
  findOne(@Param('id') id: string, @Query() query: { name: string }) {
    return this.demoService.findOne(+id, query);
  }

  // @Patch(':id')
  // update(@Param('id') id: string, @Body() updateDemoDto: UpdateDemoDto) {
  //   return this.demoService.update(+id, updateDemoDto);
  // }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.demoService.remove(+id);
  }
}
